'use strict'
var mysql = require('mysql2')
var mysqlConn = require('../conf/mysql.js').mysql
var pool = mysql.createPool(mysqlConn)
var fs = require('fs')
var path = require('path')

exports.getList = function (code, index, size) {
  console.log('params', code, index, size)
  var qryString = `SELECT * FROM stock_bulletin WHERE stkcode= '${code}' ORDER BY pubdate DESC LIMIT ${index*size},${size}`
  var promise = new Promise(function(resolve, reject) {
    pool.query(qryString, function(err, results, fields) {
      if(err) {
        reject(err)
      }else {
        resolve({
          data: results
        })
      }
    })
  })
  return promise
}

exports.getFile = function (name, date, type) {
  console.log('params', name,date,type)
  // var filePath = path.resolve('./') + '/docs/' + name+'.pdf'
  // var filePath = '\\\\192.168.31.69.245\\announcement\\sh\\2016-04-27\\1202248783.pdf'
  var filePath = `B://announcement/${type}/${date}/${name}.pdf`
  // var filePath = 'B://announcement/sh/2016-04-27/1202248783.pdf'
  console.log('filePath', filePath)
  var promise = new Promise(function(resolve, reject) {
    fs.readFile(filePath, function(error, data) {
      if(error) {
        reject(error)
      }else {
        // console.log('data',data)
        // var data = data.toString()
        resolve({
          data: data
        })
      }
    })
  })
  return promise
}